"""
查找替换嵌套的 unix_timestamp 函数
"""

import logging
import re


def change_date_sub_unix_date(m):
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))".format(
        str(m.group(5)).strip())
    return new_date_format


def change_date_sub_unix_date2(m):
    new_date_format = "floor(extract(epoch from CURRENT_DATE - INTERVAL '{} DAY'))".format(str(m.group(5)).strip())
    return new_date_format


def change_date_sub_month_unix_date(m):
    # # unix_timestamp(date_sub(now(),INTERVAL 3 MONTH))
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE - INTERVAL '{} MONTH', 'yyyyMMdd')::timestamp))".format(str(m.group(4)).strip())
    return new_date_format



def change_ut_df_date_add_now_day_hyphen(m):
    # UNIX_TIMESTAMP(date_format(date_add(now(),interval 1 day),'%Y-%m-%d'))
    logging.info("change_ut_df_date_add_now_day_hyphen")
    logging.info(m)
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY', 'yyyy-MM-dd')::timestamp))".format(str(m.group(6)).strip())
    logging.info(new_date_format)
    return new_date_format


def change_std_ds_df_2_ut(m):
    # # unix_timestamp(
    #                 str_to_date(
    #                         date_add(
    #                                        date_format(now(),'%Y-%m-%d')
    #                                        ,interval N day
    #                         )
    #                         ,'%Y-%m-%d')
    #                 )
    logging.info("change_std_ds_df_2_ut")
    logging.info(m)
    new_date_format = "FLOOR(EXTRACT(EPOCH FROM TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY', 'yyyy-MM-dd')::TIMESTAMP))".format(str(m.group(7)).strip())
    logging.info(new_date_format)
    return new_date_format

def change_std_df_now_2_ut(m):
    # # unix_timestamp(
    #                 str_to_date(
    #                         date_format(
    #                                        now()
    #                                        ,'%y-%m-%d'
    #                         )
    #                         ,'%Y-%m-%d')
    #                 )
    logging.info("change_std_df_now_2_ut")
    logging.info(m)
    new_date_format = "FLOOR(EXTRACT(EPOCH FROM CURRENT_DATE))"
    logging.info(new_date_format)
    return new_date_format


def change_now_2_ut_day_hyphen(m):
    # UNIX_TIMESTAMP(date_format(date_add(now(),interval 1 day),'%Y-%m-%d'))
    logging.info("change_now_2_ut_day_hyphen")
    logging.info(m)
    new_date_format = "floor(extract(epoch from CURRENT_DATE))"
    logging.info(new_date_format)
    return new_date_format


def change_date_add_day_unix_date(m):
    logging.info("change_date_add_day_unix_date")
    logging.info(m)
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE - INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))".format(
        str(m.group(5)).strip())
    return new_date_format


def change_date_add_unix_date(m):
    old_date_format = "{}({}({}(now(),'%Y%m%d'),{} {} {}))".format(m.group(1), m.group(2), m.group(3), m.group(4),
                                                                        m.group(5), m.group(6))
    logging.debug("原始 unix_timestamp 语句 = ")
    logging.debug(old_date_format)
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))".format(
        str(m.group(5)).strip())
    return new_date_format


def change_date_add_unix_date2(m):
    logging.info("change_date_add_unix_date2")
    logging.debug(m)
    new_date_format = "floor(extract(epoch from TO_CHAR(CURRENT_DATE + INTERVAL '{} DAY', 'yyyyMMdd')::timestamp))".format(
        str(m.group(5)).strip())
    return new_date_format


def rep_complex_fn_unix_timestamp(document_content):
    logging.info("rep_complex_fn_unix_timestamp")





    # unix_timestamp(str_to_date(date_add(date_format(now(),'%Y-%m-%d'),interval 1 day),'%Y-%m-%d'))
    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((STR_TO_DATE|str_to_date)\((DATE_ADD|date_add)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\-\%m\-\%d\'\)\,(INTERVAL|interval)([^,\(\)]*)(DAY|day)\)\,\'\%Y\-\%m\-\%d\'\)\)')
    result = unix_timestamp_patten.sub(change_std_ds_df_2_ut, document_content)
    document_content = result

    # unix_timestamp(str_to_date(date_format(now(),'%Y-%m-%d'),'%Y-%m-%d'))
    unix_timestamp_patten = re.compile(
        r"(unix_timestamp|UNIX_TIMESTAMP)\((STR_TO_DATE|str_to_date)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\-\%m\-\%d\'\)\,\'\%Y\-\%m\-\%d\'\)\)")
    result = unix_timestamp_patten.sub(change_std_df_now_2_ut, document_content)
    document_content = result


    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((date_sub|DATE_SUB)\((date_format|DATE_FORMAT)\(now\(\)\,\'\%Y\%m\%d\'\)\,(interval|INTERVAL)([^\(\)]*)(day|DAY)\)\)')
    result = unix_timestamp_patten.sub(change_date_sub_unix_date, document_content)
    document_content = result

    # unix_timestamp(date_sub(date_format(now(), '%Y-%m-%d'), interval 100 day)) * 1000
    unix_timestamp_patten = re.compile(
        r"(unix_timestamp|UNIX_TIMESTAMP)\((date_add|DATE_ADD)\((date_format|DATE_FORMAT)\(now\(\)\,\'\%Y\-\%m\-\%d\'\)\,(interval|INTERVAL)([^\(\)]*)(day|DAY)\)\)")
    result = unix_timestamp_patten.sub(change_date_add_unix_date2, document_content)
    document_content = result

    unix_timestamp_patten = re.compile(
        r"(unix_timestamp|UNIX_TIMESTAMP)\((date_sub|DATE_SUB)\((date_format|DATE_FORMAT)\(now\(\)\,\'\%Y\-\%m\-\%d\'\)\,(interval|INTERVAL)([^\(\)]*)(day|DAY)\)\)")
    result = unix_timestamp_patten.sub(change_date_sub_unix_date2, document_content)
    document_content = result

    # unix_timestamp(date_sub(now(),INTERVAL 3 MONTH))
    unix_timestamp_patten = re.compile(
        r'(unix_timestamp|UNIX_TIMESTAMP)\((date_sub|DATE_SUB)\(now\(\)\,(interval|INTERVAL)([^\(\)]*)(MONTH|month)\)\)')
    result = unix_timestamp_patten.sub(change_date_sub_month_unix_date, document_content)
    document_content = result


    # UNIX_TIMESTAMP(date_format(date_add(now(),interval 1 day),'%Y-%m-%d'))*1000
    # UNIX_TIMESTAMP(date_format(date_add(now(),interval 1 day),'%Y-%m-%d'))
    # UNIX_TIMESTAMP(date_format(date_add(now(),interval 1 day),'%Y-%m-%d'))
    unix_timestamp_patten = re.compile(
        r"(UNIX_TIMESTAMP|unix_timestamp)\((DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((NOW|now)\(\)\,(INTERVAL|interval)([^\(\)]*)(DAY|day)\)\,\'\%Y\-\%m\-\%d\'\)\)")
    result = unix_timestamp_patten.sub(change_ut_df_date_add_now_day_hyphen, document_content)
    document_content = result

    unix_timestamp_patten = re.compile(
        r"(UNIX_TIMESTAMP|unix_timestamp)\((DATE_FORMAT|date_format)\((DATE_ADD|date_add)\((NOW|now)\(\)\,(INTERVAL|interval)([^\(\)]*)(DAY|day)\)\,\'\%Y\-\%m\-\%d\'\)\)")
    result = unix_timestamp_patten.sub(change_ut_df_date_add_now_day_hyphen, document_content)
    document_content = result

    # unix_timestamp(date_add(now(), interval 1 day)) * 1000
    unix_timestamp_patten = re.compile(
        r'(UNIX_TIMESTAMP|unix_timestamp)\((DATE_FORMAT|date_format)\((NOW|now)\(\)\,\'\%Y\-\%m\-\%d\'\)\)')
    result = unix_timestamp_patten.sub(change_now_2_ut_day_hyphen, document_content)
    document_content = result

    # UNIX_TIMESTAMP(date_format(date_add(now(),interval 1 day),'%Y-%m-%d'))*1000



    unix_timestamp_patten = re.compile(
        r"(UNIX_TIMESTAMP|unix_timestamp)\((DATE_ADD|date_add)\((DATE_FORMAT|date_format)\(now\(\)\,\'\%Y\%m\%d\'\)\,(interval|INTERVAL)(.*)(day|DAY)\)\)")
    result = unix_timestamp_patten.sub(change_date_add_unix_date, document_content)
    document_content = result

    return document_content
